package com.hsmw.api.controller.driver;

import cn.hutool.json.JSONObject;
import com.aliyun.dypnsapi20170525.Client;
import com.aliyun.dypnsapi20170525.models.*;
import com.aliyun.teaopenapi.models.*;
import com.hsmw.api.controller.AuthController;
import com.htn.common.core.base.DataResponse;
import com.hsmw.common.base.Log;
import com.htn.common.core.constant.CommonConstants;
import com.hsmw.domain.HsmwUser;
import com.hsmw.domain.vo.LoginRequestVO;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;


@Api(tags = "一键登录取号")
@RestController
@RequestMapping(value = "/api/")
public class OneClickLogin {


    public static final String AccessKeyID = "LTAI5t9u19GEManwRrXQyE1R";

    public static final String AccessKeySecret = "FKwmjJCo2zdTJueFMmkAgpuijgRkpd";


    @Autowired
    private AuthController authController;

    /**
     * 使用AK&SK初始化账号Client
     *
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dypnsapi.aliyuncs.com";
        return new Client(config);
    }


    @ApiOperation(value = "一键登录", notes = "一键登录", produces = "application/json")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "App端SDK获取的登录token",name = "accessToken",dataType = "String"),
            @ApiImplicitParam(value = "类型 DRIVER FOREMAN",name = "type",dataType = "String")
    })
    @PostMapping("getMobileByAndroid")
    public DataResponse getMobileByAndroid(String accessToken,String type) throws Exception {
        Client client = OneClickLogin.createClient(AccessKeyID, AccessKeySecret);
        GetMobileRequest getMobileRequest = new GetMobileRequest().setAccessToken(accessToken);
        // 复制代码运行请自行打印 API 的返回值
        GetMobileResponse mobile = client.getMobile(getMobileRequest);
        LoginRequestVO loginRequestVO =new LoginRequestVO();
        String msg = mobile.getBody().getMessage();
        if(msg.equals("OK")){
            loginRequestVO.setUsername( mobile.getBody().getGetMobileResultDTO().getMobile());
            loginRequestVO.setType(type);
            DataResponse login = authController.login(loginRequestVO);
            return login;
        }else {
            return new DataResponse(CommonConstants.ResponseStatus.FAIL,"手机号获取失败，请重新登录");
        }





    }
}